//
//  DVSicknessItemsView.m
//  QuickCure
//
//  Created by 松鼠君 on 9/12/16.
//  Copyright © 2016 haoge. All rights reserved.
//

#import "DVSicknessItemsView.h"
#import "ViewController.h"
#import "DVSicknessJointModel.h"

@interface DVSicknessItemsView()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionViewFlowLayout* flowlayout;
@property(nonatomic,weak)   UIButton* famousDoctorBT ;
@property(nonatomic,strong) NSArray* sicknessPlist;
@end
@implementation DVSicknessItemsView
static  NSString* reusedID = @"cell";



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initializeForView];
    }
    return self;
}

//懒加载

//懒加载
-(NSArray*)sicknessPlist{
    
    if (_sicknessPlist == nil) {
        
        
        _sicknessPlist = [NSArray arrayWithArray:[DVSicknessJointModel sicknessJointWithFile:[[NSBundle mainBundle] pathForResource:@"SicknessJoint.plist" ofType:nil]]];
        
    }
    
    return _sicknessPlist;
    
}


//懒加载
-(NSMutableArray*)labelStrArr{
    
    
    if (!_labelStrArr ) {
        
        
        _labelStrArr = [NSMutableArray array];
        
    }
    
    return _labelStrArr;
    
}

-(void)initializeForView{
    
    
    
    self. flowlayout  = [UICollectionViewFlowLayout new];
    
    UICollectionView* collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) collectionViewLayout:self.flowlayout];
    
    self.collectionView = collection;
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:collection];
    
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reusedID];
    
    collection.dataSource = self;
    
    
    
    
    
    UIButton* famousDoctorBT = [[UIButton alloc]init];
    
    self.famousDoctorBT = famousDoctorBT;
    
    [famousDoctorBT setTitle:@"名医通" forState: UIControlStateNormal];
    
    famousDoctorBT. titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0,-10);
    
    [famousDoctorBT setImage:[UIImage imageNamed:@"yj"] forState:UIControlStateNormal];
    
    
    [self addSubview:famousDoctorBT];
    
    [famousDoctorBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
     [famousDoctorBT mas_makeConstraints:^(MASConstraintMaker *make) {
         
         make.left.top.equalTo(self).offset(15);
     }];
    
    
    
}


-(void)layoutSubviews{
    
    if (self.bounds.size.width) {
        
        
        self.collectionView.frame = CGRectMake(0, self.famousDoctorBT.bounds.size.height+50, self.bounds.size.width, self.bounds.size.height-self.famousDoctorBT.bounds.size.height-50);
        
        self.flowlayout.itemSize =CGSizeMake(self.collectionView.bounds.size.width/3.00470, self.collectionView.bounds.size.height/2);
        
        self.flowlayout.minimumInteritemSpacing = 0;
        
        self.flowlayout.minimumLineSpacing = 0 ;
        
        
    }


    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 6;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DVSicknessJointModel* model = self.sicknessPlist[indexPath.row];
    
    UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:reusedID forIndexPath:indexPath];
    
    
    
    
    cell.layer.borderColor=[UIColor grayColor].CGColor;
    
    cell.layer.borderWidth= 0.5;
    
    
    UIImageView* imgV= [[UIImageView alloc]initWithImage:[UIImage imageNamed:model.img_name]];
    
    [cell addSubview:imgV];
    
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(cell);
        
        make.top.lessThanOrEqualTo(cell).offset(20);
        
        make.top.greaterThanOrEqualTo(cell).offset(10);
        
        make.size.equalTo(CGSizeMake(70, 70));
    }];
    
    
    UILabel* label = [UILabel new];
    
    [cell addSubview:label];
    
    label.text = model.title;
    
    
    [self.labelStrArr addObject:label.text];
    
    
    label.textAlignment = NSTextAlignmentCenter;
    
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(cell);
        
        
        make.bottom.lessThanOrEqualTo(cell).offset(-10).priority(10);
        
        make.bottom.greaterThanOrEqualTo(cell).offset(-20).priority(10);

        make.top.lessThanOrEqualTo(imgV.mas_bottom).offset(15);
        
        make.top.greaterThanOrEqualTo(imgV.mas_bottom).offset(5);


    }];
    
    
    
    
    return cell;
    
}






- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
}



@end
